package com.tiennt.android.callnotes.models.listadapters;

import java.util.ArrayList;

import com.tiennt.android.callnotes.R;
import com.tiennt.android.callnotes.models.contentholders.ContactInfo;
import com.tiennt.android.callnotes.models.viewholders.ListContactsViewHolder;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;

public class ListContactsAdapter extends ArrayAdapter<ContactInfo> {

	private Context mContext;
	private ArrayList<ContactInfo> mListItems;
	private ArrayList<ContactInfo> mOriginListItems;

	public ListContactsAdapter(Context context, int textViewResourceId,
			ArrayList<ContactInfo> objects) {
		super(context, textViewResourceId, objects);
		mContext = context;
		mListItems = objects;
		mOriginListItems = mListItems;
		// TODO Auto-generated constructor stub
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListItems.size();
	}

	@Override
	public ContactInfo getItem(int position) {
		// TODO Auto-generated method stub
		return mListItems.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		ListContactsViewHolder viewHolder = null;

		if (v == null) {
			v = LayoutInflater.from(mContext).inflate(
					R.layout.list_contacts_row, null);
			viewHolder = new ListContactsViewHolder(v);
			v.setTag(viewHolder);
		} else {
			viewHolder = (ListContactsViewHolder) v.getTag();
		}

		final ContactInfo item = mListItems.get(position);

		if (item != null) {
			viewHolder.getImgvContactPhoto().setImageBitmap(
					item.getContactPhoto());
			viewHolder.getTvContactName().setText(item.getContactName());
		}
		return v;
	}

	public void removeAt(int index) {
		if (index >= mListItems.size()) {
			return;
		}
		mListItems.remove(index);
		notifyDataSetChanged();
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return new Filter() {

			@SuppressWarnings("unchecked")
			@Override
			protected void publishResults(CharSequence constraint,
					FilterResults results) {
				// TODO Auto-generated method stub
				mListItems = (ArrayList<ContactInfo>) results.values;
				notifyDataSetChanged();
			}

			@Override
			protected FilterResults performFiltering(CharSequence constraint) {
				// TODO Auto-generated method stub
				ArrayList<ContactInfo> filteredResults = new ArrayList<ContactInfo>();

				mListItems = mOriginListItems;
				for (ContactInfo item : mListItems) {
					if (item.getContactName().toUpperCase()
							.contains(constraint.toString().toUpperCase())) {
						filteredResults.add(item);
					}
				}

				FilterResults results = new FilterResults();
				results.values = filteredResults;

				return results;
			}
		};
	}

}
